package com.xj.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xj.reggie.common.R;
import com.xj.reggie.dto.DishDto;
import com.xj.reggie.dto.SetmealDto;
import com.xj.reggie.entity.Category;
import com.xj.reggie.entity.Dish;
import com.xj.reggie.entity.Setmeal;
import com.xj.reggie.entity.SetmealDish;
import com.xj.reggie.service.CategoryService;
import com.xj.reggie.service.DishService;
import com.xj.reggie.service.SetmealDishService;
import com.xj.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author XJ
 * @Date 2022/10/20 16:05
 * @PackageName:com.xj.reggie.controller
 * @ClassName: SetmealController
 * @Description: 掏槽管理
 * @Version 1.0
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

	@Autowired
	private SetmealService setmealService;

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private SetmealDishService setmealDishService;

	@Autowired
	private DishService dishService;

	/**
	 * 新增套餐
	 * @param setmealDto
	 * @return
	 */
	@PostMapping
	public R<String> save(@RequestBody SetmealDto setmealDto){
		log.info("套餐信息：{}",setmealDto);

		setmealService.saveWithDish(setmealDto);

		return R.success("新增套餐成功！");
	}

	@GetMapping("/page")
	public R<Page> page(int page,int pageSize,String name){
		//分页构造器对象
		Page<Setmeal> pageInfo = new Page<>(page,pageSize);
		Page<SetmealDto> dtoPage = new Page<>();

		LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
		//添加查询条件,根据name进行like模糊查询
		queryWrapper.like(name !=null,Setmeal::getName,name);
		//添加排序条件，根据更新时间降序排列
		queryWrapper.orderByDesc(Setmeal::getUpdateTime);

		setmealService.page(pageInfo,queryWrapper);

		//对象拷贝
		BeanUtils.copyProperties(pageInfo,dtoPage,"records");
		List<Setmeal> records = pageInfo.getRecords();

		List<SetmealDto> list = records.stream().map((item)->{
			SetmealDto setmealDto = new SetmealDto();
			//对象拷贝
			BeanUtils.copyProperties(item,setmealDto);
			//分类id
			Long categoryId = item.getCategoryId();
			//根据分类id查询分类
			Category category = categoryService.getById(categoryId);
			if (category !=null){
				//分类名称
				String categoryName = category.getName();
				setmealDto.setCategoryName(categoryName);
			}
			return setmealDto;
		}).collect(Collectors.toList());

		dtoPage.setRecords(list);
		return R.success(dtoPage);
	}

	/**
	 * 删除套餐
	 * @param ids
	 * @return
	 */
	@DeleteMapping
	public R<String> delete(@RequestParam List<Long> ids){
		log.info("ids:{}",ids);

		setmealService.removeWithDish(ids);
		return R.success("套餐数据删除成功");
	}

	/**
	 * 根据条件查询套餐数据
	 * @param setmeal
	 * @return
	 */
	@GetMapping("/list")
	private R<List<Setmeal>> list(Setmeal setmeal){
		LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(setmeal.getCategoryId()!=null,
				Setmeal::getCategoryId,setmeal.getCategoryId());

		queryWrapper.eq(setmeal.getStatus()!=null,
				Setmeal::getStatus,setmeal.getStatus());

		queryWrapper.orderByDesc(Setmeal::getUpdateTime);

		List<Setmeal> list = setmealService.list(queryWrapper);

		return R.success(list);
	}

	/**
	 * 对菜品批量或者是单个 进行停售或者是起售
	 * @return
	 */
	@PostMapping("/status/{status}")
//这个参数这里一定记得加注解才能获取到参数，否则这里非常容易出问题
	public R<String> status(@PathVariable("status") Integer status,@RequestParam List<Long> ids){
		setmealService.updateSetmealStatusById(status,ids);
		return R.success("售卖状态修改成功");
	}

	/**
	 * 回显套餐数据：根据套餐id查询套餐
	 * @return
	 */
	@GetMapping("/{id}")
	public R<SetmealDto> getData(@PathVariable Long id){
		SetmealDto setmealDto = setmealService.getDate(id);

		return R.success(setmealDto);
	}

	@PutMapping
	public R<String> edit(@RequestBody SetmealDto setmealDto){

		if (setmealDto==null){
			return R.error("请求异常");
		}

		if (setmealDto.getSetmealDishes()==null){
			return R.error("套餐没有菜品,请添加套餐");
		}
		List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
		Long setmealId = setmealDto.getId();

		LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
		setmealDishService.remove(queryWrapper);

		//为setmeal_dish表填充相关的属性
		for (SetmealDish setmealDish : setmealDishes) {
			setmealDish.setSetmealId(setmealId);
		}
		//批量把setmealDish保存到setmeal_dish表
		setmealDishService.saveBatch(setmealDishes);
		setmealService.updateById(setmealDto);

		return R.success("套餐修改成功");
	}


	/**
	 * 移动端点击套餐图片查看套餐具体内容
	 * 这里返回的是dto 对象，因为前端需要copies这个属性
	 * 前端主要要展示的信息是:套餐中菜品的基本信息，图片，菜品描述，以及菜品的份数
	 * @param SetmealId
	 * @return
	 */
	//这里前端是使用路径来传值的，要注意，不然你前端的请求都接收不到，就有点尴尬哈
	@GetMapping("/dish/{id}")
	public R<List<DishDto>> dish(@PathVariable("id") Long SetmealId){
		LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SetmealDish::getSetmealId,SetmealId);
		//获取套餐里面的所有菜品  这个就是SetmealDish表里面的数据
		List<SetmealDish> list = setmealDishService.list(queryWrapper);

		List<DishDto> dishDtos = list.stream().map((setmealDish) -> {
			DishDto dishDto = new DishDto();
			//其实这个BeanUtils的拷贝是浅拷贝，这里要注意一下
			BeanUtils.copyProperties(setmealDish, dishDto);
			//这里是为了把套餐中的菜品的基本信息填充到dto中，比如菜品描述，菜品图片等菜品的基本信息
			Long dishId = setmealDish.getDishId();
			Dish dish = dishService.getById(dishId);
			BeanUtils.copyProperties(dish, dishDto);

			return dishDto;
		}).collect(Collectors.toList());

		return R.success(dishDtos);
	}
}
